{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['instant', 'dteday', 'season', 'yr', 'mnth', 'holiday', 'weekday',\n       'workingday', 'weathersit', 'temp', 'atemp', 'hum', 'windspeed',\n       'casual', 'registered', 'cnt'],\n      dtype='object')\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "filename = \"data/bike_sharing/day.csv\"\n",
    "train = pd.read_csv(filename)\n",
    "print(train.columns)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将特征进行分组\n",
    "instant_features = [\"instant\"]\n",
    "categorical_features_ready = [\"yr\", \"holiday\", \"workingday\"]\n",
    "categorical_features_dummy = [\"season\", \"mnth\", \"weekday\", \"weathersit\"]\n",
    "statistical_features = [\"temp\", \"atemp\", \"hum\", \"windspeed\"]\n",
    "y = [\"cnt\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n不需要特征编码的离散字段\n    yr  holiday  workingday\n0   0        0           0\n1   0        0           0\n2   0        0           1\n\n经过特征编码的离散字段\n    season_1  season_2  season_3  season_4  mnth_1  mnth_2  mnth_3  mnth_4  \\\n0         1         0         0         0       1       0       0       0   \n1         1         0         0         0       1       0       0       0   \n2         1         0         0         0       1       0       0       0   \n\n   mnth_5  mnth_6  ...  weekday_0  weekday_1  weekday_2  weekday_3  weekday_4  \\\n0       0       0  ...          0          0          0          0          0   \n1       0       0  ...          1          0          0          0          0   \n2       0       0  ...          0          1          0          0          0   \n\n   weekday_5  weekday_6  weathersit_1  weathersit_2  weathersit_3  \n0          0          1             0             1             0  \n1          0          0             0             1             0  \n2          0          0             1             0             0  \n\n[3 rows x 26 columns]\n"
     ]
    }
   ],
   "source": [
    "# 离散特征里\n",
    "# 不需要特征编码的字段 yr, holiday, workingday\n",
    "X_train_cat_ready = train[categorical_features_ready]\n",
    "print(\"\\n不需要特征编码的离散字段\\n\", X_train_cat_ready.head(3))\n",
    "\n",
    "# 需要特征编码的字段\n",
    "for col in categorical_features_dummy:\n",
    "    train[col] = train[col].astype(\"object\")\n",
    "    \n",
    "X_train_cat_dummy = pd.get_dummies(train[categorical_features_dummy])\n",
    "print(\"\\n经过特征编码的离散字段\\n\", X_train_cat_dummy.head(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       temp     atemp       hum  windspeed\n0  0.355170  0.373517  0.828620   0.284606\n1  0.379232  0.360541  0.715771   0.466215\n2  0.171000  0.144830  0.449638   0.465740\n"
     ]
    }
   ],
   "source": [
    "from sklearn.preprocessing import MinMaxScaler\n",
    "\n",
    "# 连续特征，进行归一化\n",
    "scaler = MinMaxScaler()\n",
    "temp = scaler.fit_transform(train[statistical_features])\n",
    "X_train_statistical = pd.DataFrame(data=temp, columns=statistical_features, index=train.index)\n",
    "print(X_train_statistical.head(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   instant  yr  holiday  workingday  season_1  season_2  season_3  season_4  \\\n0        1   0        0           0         1         0         0         0   \n1        2   0        0           0         1         0         0         0   \n2        3   0        0           1         1         0         0         0   \n\n   mnth_1  mnth_2  ...  weekday_5  weekday_6  weathersit_1  weathersit_2  \\\n0       1       0  ...          0          1             0             1   \n1       1       0  ...          0          0             0             1   \n2       1       0  ...          0          0             1             0   \n\n   weathersit_3      temp     atemp       hum  windspeed   cnt  \n0             0  0.355170  0.373517  0.828620   0.284606   985  \n1             0  0.379232  0.360541  0.715771   0.466215   801  \n2             0  0.171000  0.144830  0.449638   0.465740  1349  \n\n[3 rows x 35 columns]\n"
     ]
    }
   ],
   "source": [
    "X_train = pd.concat([train[instant_features], X_train_cat_ready, X_train_cat_dummy, X_train_statistical, train[y]], \n",
    "                    axis=1, ignore_index=False)\n",
    "print(X_train.head(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   instant  yr  holiday  workingday  season_1  season_2  season_3  season_4  \\\n0        1   0        0           0         1         0         0         0   \n1        2   0        0           0         1         0         0         0   \n2        3   0        0           1         1         0         0         0   \n3        4   0        0           1         1         0         0         0   \n4        5   0        0           1         1         0         0         0   \n\n   mnth_1  mnth_2  ...  weekday_5  weekday_6  weathersit_1  weathersit_2  \\\n0       1       0  ...          0          1             0             1   \n1       1       0  ...          0          0             0             1   \n2       1       0  ...          0          0             1             0   \n3       1       0  ...          0          0             1             0   \n4       1       0  ...          0          0             1             0   \n\n   weathersit_3      temp     atemp       hum  windspeed   cnt  \n0             0  0.355170  0.373517  0.828620   0.284606   985  \n1             0  0.379232  0.360541  0.715771   0.466215   801  \n2             0  0.171000  0.144830  0.449638   0.465740  1349  \n3             0  0.175530  0.174649  0.607131   0.284297  1562  \n4             0  0.209120  0.197158  0.449313   0.339143  1600  \n\n[5 rows x 35 columns]\n"
     ]
    }
   ],
   "source": [
    "outfilename = \"out/bike_sharing/FE_day.csv\"\n",
    "X_train.to_csv(outfilename, index=False)\n",
    "print(X_train.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
